package com.jxb.myshop.web.servlet;

import com.jxb.myshop.common.Constant;
import com.jxb.myshop.common.ResponseResult;
import com.jxb.myshop.entity.Address;
import com.jxb.myshop.entity.User;
import com.jxb.myshop.services.AddressServices;
import com.jxb.myshop.services.impl.AddressServicesImpl;
import com.jxb.myshop.util.StringUtil;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;

@WebServlet("/address")
public class AddressController extends BaseController {
    private AddressServices as = new AddressServicesImpl();

    /**
     * 修改用户默认地址
     */
    public String updateAddressDefault(HttpServletRequest req, HttpServletResponse resp){
        User user = (User) req.getSession().getAttribute(Constant.SystemConstant.IS_LOGIN);
        String aid = req.getParameter("aid");
        ResponseResult rr = as.updateAddressDefault(StringUtil.StringToInt(aid),user.getUid());
        req.setAttribute("rr",rr);
        return Constant.Re.URL_FORWARD + "/address?method=getUserAddress";
    }

    /**
     * 修改用户地址
     */
    public String updateAddress(HttpServletRequest req, HttpServletResponse resp) throws InvocationTargetException, IllegalAccessException {
        User user = (User) req.getSession().getAttribute(Constant.SystemConstant.IS_LOGIN);
        // 从前端表单中获取值，封装成一个map集合
        Map<String, String[]> addMap = req.getParameterMap();
        //创建需要封装数据的对象
        Address a = new Address();
        //使用工具类进行封装
        BeanUtils.populate(a, addMap);
        // 把封装好的对象传入servlet中
        ResponseResult rr = as.updateAddress(a);
        return Constant.Re.URL_FORWARD + "/address?method=getUserAddress";
    }

    /**
     * 删除用户收货地址
     */
    public String deleteAddress(HttpServletRequest req, HttpServletResponse resp){
        User user = (User) req.getSession().getAttribute(Constant.SystemConstant.IS_LOGIN);
        int aid = StringUtil.StringToInt(req.getParameter("aid"));
        ResponseResult rr = as.deleteAddress(aid);
        return Constant.Re.URL_FORWARD + "/address?method=getUserAddress";
    }

    /**
     * 新增用户收货地址
     */
    public String addAddress(HttpServletRequest req, HttpServletResponse resp){
        User user = (User) req.getSession().getAttribute(Constant.SystemConstant.IS_LOGIN);
        String name = req.getParameter("aname");
        String phone = req.getParameter("aphone");
        String detail = req.getParameter("adetail");
        Address add = new Address(user.getUid(),name,phone,detail);
        ResponseResult rr = as.addAddress(add);
        return Constant.Re.URL_REDIRECT + "/address?method=getUserAddress";
    }

    /**
     * 获取用户收货地址
     * @param req
     * @param resp
     * @return
     */
    public String getUserAddress(HttpServletRequest req, HttpServletResponse resp){
        User user = (User) req.getSession().getAttribute(Constant.SystemConstant.IS_LOGIN);
        if (user == null) {
            return Constant.Re.URL_REDIRECT + "/login.jsp";
        }
        ResponseResult rr = as.getUserAddress(user.getUid());
        req.setAttribute("rr",rr);
        return Constant.Re.URL_FORWARD + "/self_info.jsp";
    }
}
